<?php

namespace app\admin\model\air;

use think\Model;
use app\admin\model\Area;


class Charter extends Model
{

    

    

    // 表名
    protected $name = 'air_charter';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'flight_time_text',
        'status_text'
    ];
    
    public function getUnitList()
    {
        return ['rmb' => __('人民币'), 'us' => __('美元')];
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }

    public function getCitysList()
    {
        return ['in' => __('国内/港澳台'), 'out' => __('国际城市')];
    }


    public function getFlightTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flight_time']) ? $data['flight_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setFlightTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function scity()
    {
        return $this->belongsTo(Area::class, 'start_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function ecity()
    {
        return $this->belongsTo(Area::class, 'end_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
